import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        title: '首页 - TechNova科技资讯'
      }
    },
    {
      path: '/article/:id',
      name: 'article',
      component: () => import('../views/ArticleView.vue'),
      meta: {
        title: '文章详情 - TechNova科技资讯'
      }
    },
    {
      path: '/category/:category',
      name: 'category',
      component: () => import('../views/CategoryView.vue'),
      meta: {
        title: '分类浏览 - TechNova科技资讯'
      }
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/SearchView.vue'),
      meta: {
        title: '搜索结果 - TechNova科技资讯'
      }
    },
    {
      path: '/ai',
      redirect: '/category/AI'
    },
    {
      path: '/blockchain',
      redirect: '/category/Blockchain'
    },
    {
      path: '/hardware',
      redirect: '/category/Hardware'
    },
    {
      path: '/startup',
      redirect: '/category/Startup'
    },
    {
      path: '/mobile',
      redirect: '/category/Mobile'
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
      meta: {
        title: '关于我们 - TechNova科技资讯'
      }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('../views/NotFoundView.vue'),
      meta: {
        title: '页面未找到 - TechNova科技资讯'
      }
    }
  ],
})

// 路由守卫 - 更新页面标题
router.beforeEach((to, from, next) => {
  if (to.meta?.title) {
    document.title = to.meta.title as string
  }
  next()
})

export default router
